/**
 * @Copyright: 2016 杭州海康威视系统技术有限公司
 * @address: http://www.hikvision.com
 * @ProjectName: itms V1.0
 * @Description: 红外热成像测温系统
 */
package com.hlkj.baojin.utils.hik;

import com.ivms6.core.util.cm.ConfigManager;
import com.ivms6.core.util.cm.Section;

/**
 * <p>
 * </p>
 * 
 * @author chixueshun 2014-4-16 下午2:19:29
 * @version V1.0
 */
public class CfgMgr extends ConfigManager {

	public static final String CONFIG_FILE = "classpath:system.properties";
	
	public static final String K_PLATFORM_SDK ="platformSdk";
	
	public static final String K_PLATFORM_SDK_IS_CHOOSE="platformSdk.is_choose";

	public static final String K_CMS = "cms";

	public static final String K_VMS = "vms";

	public static final String K_MQ = "mq";

	public static final String K_SERVICE = "service";

	public static final String K_CMS_IP = "cms.ip";

	public static final String K_CMS_WS_PORT = "cms.ws_port";

	public static final String K_CMS_CONTEXT_PATH = "cms.context_path";

	public static final String K_VMS_IP = "vms.ip";

	public static final String K_VMS_WS_PORT = "vms.ws_port";

	public static final String K_VMS_CONTEXT_PATH = "vms.context_path";

	public static final String K_SERVICE_INDEX_CODE = "service.index_code";

	public static final String K_SERVICE_CACHE_TIMEOUT = "service.cache_timeout";

	public static final String K_SERVICE_CONTEXT_PATH = "service.context_path";

	public static final String K_SERVICE_WS_PORT = "service.ws_port";

	public static final String K_MQ_BROKE_URL = "mq.broke_url";

	public static final String K_MQ_TYPE = "mq.type";

	public static final String K_MQ_FACTORY_NAME = "mq.factory_name";

	public static final String K_SYS_PARAM = "sysparam";
	
	public static final String K_SERVICE_PARAM ="service-param";

	public static final String K_SYS_PARAM_APP_PATH = "install.app.path";

	public static final String K_SYS_PARAM_INSTALL_IP = "install.ivms.ip";
	

	public static final String K_DRUID_PARAM = "druid-param";

	public static final String K_DRUID_PARAM_ALLOW = "druid.allow";

	public static final String K_LOGIN_LOCK_IS_OPEN = "login.lock.is_open";

	public static final String K_LOGIN_LOCK_TIMES = "login.lock.login_times";

	public static final String K_LOGIN_LOCK_LOCKED_TIME = "login.lock.locked_time";

	public static final String K_LOG_SAVE_TIME = "log.save_time";

    public static final String K_OCX ="ocx";
    
    public static final String K_PREVIEW_OCX_THEME ="previewOcx.theme";
	
    public static final String K_PLYBACK_OCX_THEME ="playbackOcx.theme";


    public static final String K_PROTOCOL ="net.protocol";


    

    public static Section getOcxCfg() {
		return getConfig().getSection("K_OCX");
	}
    public static String getPreviewOcxTheme(){
    	return getOcxCfg().get(K_PREVIEW_OCX_THEME);
    }

	public static String getPlaybackOcxTheme(){
		return getOcxCfg().get(K_PLYBACK_OCX_THEME);
	}
	public static void setPreviewOcxTheme(String theme){
		updateOcxCfg(K_PREVIEW_OCX_THEME, theme);		
	}
	public static void setPlaybackOcxTheme(String theme){
		updateOcxCfg(K_PLYBACK_OCX_THEME, theme);		
	}
	public static Section getCms() {
		return getConfig().getSection("cms");
	}
	public static Section getPlatformSdk(){
		return getConfig().getSection(K_PLATFORM_SDK);
	}
	public static Section getServiceParam(){
		return getConfig().getSection(K_SERVICE_PARAM);
	}
	
	public static boolean getPlatformSdkIsChoose(){
		Section platformSdk = getPlatformSdk();
		if (platformSdk != null) {
			if ("true".equals(platformSdk.get(K_PLATFORM_SDK_IS_CHOOSE))) {
				return true;
			}
		}
		return false;
	}
	public static void setPlatformSdkIsChoose(boolean bool){
		if(bool){
			updatePlatformSdkCfg(K_PLATFORM_SDK_IS_CHOOSE, "true");
		}else{
			updatePlatformSdkCfg(K_PLATFORM_SDK_IS_CHOOSE, "false");
		}
	}
	
    public static String getProtocol() {
    	String protocol =getServiceParam().get(K_PROTOCOL);
    	if(!"https".equals(protocol)){
    		protocol ="http";
    	}
    	 return protocol;
 }
    public static void setProtocol(String protocol){
    	updateServiceParamCfg(K_PROTOCOL, protocol);
    }
	public static Section getVms() {
		return getConfig().getSection("vms");
	}

	public static Section getService() {
		return getConfig().getSection("service");
	}

	public static Section getMq() {
		return getConfig().getSection("mq");
	}

	public static Section getSysParam() {
		return getConfig().getSection(K_SYS_PARAM);
	}
	

	public static Section getDruidParam() {
		return getConfig().getSection(K_DRUID_PARAM);
	}

	public static Section getLoginLock() {
		return getConfig().getSection("login");
	}

	public static Section getLog() {
		return getConfig().getSection("log");
	}
	
	public static Section getAxisTemp() {
		return getConfig().getSection("axis-temp");
	}

	public static void updateServiceParamCfg( String propertyName, String propertyValue) {
		updateConfiguration(CONFIG_FILE, K_SERVICE_PARAM, propertyName, propertyValue);
		getServiceParam().put(propertyName, propertyValue);
		
	}
	public static void updateConfiguration(String sectionName, String propertyName, String propertyValue) {
		updateConfiguration(CONFIG_FILE, sectionName, propertyName, propertyValue);
	}

	public static void updateCmsCfg(String propertyName, String propertyValue) {
		updateConfiguration(CONFIG_FILE, K_CMS, propertyName, propertyValue);
		getCms().put(propertyName, propertyValue);
	}
	public static void updatePlatformSdkCfg(String propertyName, String propertyValue) {
		updateConfiguration(CONFIG_FILE, K_PLATFORM_SDK, propertyName, propertyValue);
		getPlatformSdk().put(propertyName, propertyValue);
	}
	public static void updateOcxCfg(String propertyName, String propertyValue) {
		updateConfiguration(CONFIG_FILE, K_OCX, propertyName, propertyValue);
		getOcxCfg().put(propertyName, propertyValue);
	}
	public static void updateVmsCfg(String propertyName, String propertyValue) {
		updateConfiguration(CONFIG_FILE, K_VMS, propertyName, propertyValue);
		getVms().put(propertyName, propertyValue);
	}

	public static void updateServiceCfg(String propertyName, String propertyValue) {
		updateConfiguration(CONFIG_FILE, K_SERVICE, propertyName, propertyValue);
		getService().put(propertyName, propertyValue);
	}

	public static void updateMqCfg(String propertyName, String propertyValue) {
		updateConfiguration(CONFIG_FILE, K_MQ, propertyName, propertyValue);
		getMq().put(propertyName, propertyValue);
	}

	public static String getCmsIp() {
		return getCms().get(K_CMS_IP);
	}

	public static void setCmsIp(String cmsIp) {
		updateCmsCfg(K_CMS_IP, cmsIp);
	}

	public static String getCmsContextPath() {
		return StringUtils.safeNullValue(getCms().get(K_CMS_CONTEXT_PATH));
	}

	public static void setCmsContextPath(String cmsContextPath) {
		updateCmsCfg(K_CMS_CONTEXT_PATH, cmsContextPath);
	}

	public static Integer getCmsWsPort() {
		try {
			return Integer.parseInt(getCms().get(K_CMS_WS_PORT));
		} catch (Exception e) {
			LogUtil.logError(CfgMgr.class, e);
		}
		return 80;
	}



	public static void setCmsWsPort(Integer cmsWsPort) {
		updateCmsCfg(K_CMS_WS_PORT, cmsWsPort == null ? "80" : cmsWsPort.toString());
	}

	public static String getVmsIp() {
		Section vms = getVms();
		if (vms != null) {
			return getVms().get(K_VMS_IP);
		}
		return "";
	}

	public static void setVmsIp(String vmsIp) {
		updateVmsCfg(K_VMS_IP, vmsIp);
	}

	public static String getVmsContextPath() {
		Section vms = getVms();
		if (vms != null) {
			return getVms().get(K_VMS_CONTEXT_PATH);
		}
		return "";
	}

	public static void setVmsContextPath(String vmsContextPath) {
		updateVmsCfg(K_VMS_CONTEXT_PATH, vmsContextPath);
	}

	public static Integer getVmsWsPort() {
		Section vms = getVms();
		if (vms != null) {
			try {
				return Integer.parseInt(getVms().get(K_VMS_WS_PORT));
			} catch (Exception e) {
				LogUtil.logError(CfgMgr.class, e);
			}
		}
		return 80;
	}

	public static void setVmsWsPort(Integer vmsWsPort) {
		updateVmsCfg(K_VMS_WS_PORT, vmsWsPort == null ? "80" : vmsWsPort.toString());
	}

	public static String getServiceContextPath() {
		return StringUtils.safeNullValue(getService().get(K_SERVICE_CONTEXT_PATH));
	}

	public static void setServiceContextPath(String serviceContextPath) {
		updateServiceCfg(K_SERVICE_CONTEXT_PATH, serviceContextPath);
	}

	public static String getServiceIndexCode() {
		return StringUtils.safeNullValue(getService().get(K_SERVICE_INDEX_CODE));
	}

	public static void setServiceIndexCode(String indexCode) {
		updateServiceCfg(K_SERVICE_INDEX_CODE, indexCode);
	}

	public static void setServiceWsPort(String wsPort) {
		updateServiceCfg(K_SERVICE_WS_PORT, wsPort);
	}

	public static Long getServiceCacheTimeout() {
		try {
			return Long.parseLong(getService().get(K_SERVICE_CACHE_TIMEOUT));
		} catch (Exception e) {
			LogUtil.logError(CfgMgr.class, e);
		}
		return 50000l;
	}

	public static String getMqBrokeUrl() {
		return getMq().get(K_MQ_BROKE_URL);
	}

	public static void setMqBrokeUrl(String brokeUrl) {
		updateMqCfg(K_MQ_BROKE_URL, brokeUrl);
	}

	public static String getMqType() {
		Section mq = getMq();
		if (mq != null) {
			return mq.get(K_MQ_TYPE);
		}
		return "";
	}

	public static void setMqType(String mqType) {
		updateMqCfg(K_MQ_TYPE, mqType);
	}

	public static String getMqFactoryName() {
		Section mq = getMq();
		if (mq != null) {
			return mq.get(K_MQ_FACTORY_NAME);
		}
		return "";
	}

	public static void setMqFactoryName(String mqFactoryName) {
		updateMqCfg(K_MQ_FACTORY_NAME, mqFactoryName);
	}

	/**
	 * 获取应用路径
	 * 
	 * @param:
	 * @return:
	 * @author:lidejun
	 * @date:2015-8-28
	 */
	public static String getAppPath() {
		Section sysParam = getSysParam();

		if (sysParam != null) {
			return sysParam.get(K_SYS_PARAM_APP_PATH);
		}

		return "";
	}

	public static String getInstallPath() {
		Section sysParam = getSysParam();

		if (sysParam != null) {
			return sysParam.get(K_SYS_PARAM_INSTALL_IP);
		}

		return "";
	}


	public static String getDruidAllowList() {
		Section druidParam = getDruidParam();

		if (druidParam != null) {
			return druidParam.get(K_DRUID_PARAM_ALLOW);
		}

		return "";
	}

	public static boolean getLoginLockIsOpen() {
		Section loginLock = getLoginLock();
		if (loginLock != null) {
			if ("true".equals(loginLock.get(K_LOGIN_LOCK_IS_OPEN))) {
				return true;
			}
		}
		return false;
	}

	public static void setLoginLockIsOpen(boolean isOpen) {
		if (isOpen) {
			updateVmsCfg(K_LOGIN_LOCK_IS_OPEN, "true");
		} else {
			updateVmsCfg(K_LOGIN_LOCK_IS_OPEN, "false");
		}
	}

	public static Integer getLoginLockTimes() {
		Section loginLock = getLoginLock();
		if (loginLock != null) {
			try {
				return Integer.parseInt(loginLock.get(K_LOGIN_LOCK_TIMES));
			} catch (Exception e) {
				LogUtil.logError(CfgMgr.class, e);
			}
		}
		return 80;
	}

	public static void setLoginLockTimes(Integer times) {
		updateVmsCfg(K_LOGIN_LOCK_TIMES, times == null ? "5" : times.toString());
	}

	public static Integer getLoginLockedTime() {
		Section loginLock = getLoginLock();
		if (loginLock != null) {
			try {
				return Integer.parseInt(ConfigUtil.getFileIO(K_LOGIN_LOCK_LOCKED_TIME, "/resource/system.properties"));
			} catch (Exception e) {
				LogUtil.logError(CfgMgr.class, e);
			}
		}
		return 80;
	}

	public static void setLoginLockedTime(Integer time) {
		updateVmsCfg(K_LOGIN_LOCK_LOCKED_TIME, time == null ? "10" : time.toString());
	}

	public static Integer getLogSaveTime() {
		Section log = getLog();
		if (log != null) {
			try {
				return Integer.parseInt(log.get(K_LOG_SAVE_TIME));
			} catch (Exception e) {
				LogUtil.logError(CfgMgr.class, e);
			}
		}
		return 80;
	}

	public static void setLogSaveTime(Integer saveTime) {
		updateVmsCfg(K_LOG_SAVE_TIME, saveTime == null ? "7" : saveTime.toString());
	}







	
	public static String getInspectReportPath() {
		String classPath =CfgMgr.class.getResource ("").getFile ();
		String appPath=getAppPath();
		if (appPath == null || appPath.equals("")) {
			return "";
		}
		if (classPath == null || classPath.equals("")) {
			return "";
		}
		String path = classPath.replace("WEB-INF/classes/com/hikvision/itms/util/", "");
		String inspectReportPath=path+ "static/report";
        inspectReportPath = inspectReportPath.substring(1, inspectReportPath.length());
		
		return inspectReportPath;
	}
}
